home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / music / stsynth.arc / SOUNDKEY.C next >
C/C++ Source or Header  |  1987-05-18  |  29KB  |  699 lines

  1. /*        SoundKey.c
  2.           David T. Jarvis
  3.           Atari ST -- Lattice C -- Version 052686
  4. */
  5.  
  6. #define SPACE_K           32            /* space key                    */
  7. #define ESC_K             27            /* ESC key                      */
  8. #define RETURN_K          13            /* return key                   */
  9. #define DURA            1000            /* note duration                */
  10. #define TUNE_INC           6            /* adjust tuning by this much   */
  11. #define MAX_OCT            6            /* maximum octave               */
  12. #define MAX_VOL           15            /* maximum volume               */
  13. #define MAX_WAV           15            /* maximum waveform             */
  14. #define N_P_OCT           15            /* notes per octave             */
  15. #define MOUS_OFF         256            /* disable mouse                */
  16. #define MOUS_ON          257            /* enable mouse                 */
  17. #define T_OUTLINE       0x10            /* text style--outline          */
  18. #define T_NORMAL         0x0            /* text style--normal           */
  19. #define T_SKEWED      0x0004            /* text style--skewed           */
  20. #define T_THICK       0X0001            /* text style--thick            */
  21. #define CH_ENA_R           7            /* voice channel enable(read)   */
  22. #define CH_ALO_W         128            /* channel,voice a lo,write     */
  23. #define CH_AHI_W         129            /* channel,voice a hi,write     */
  24. #define CH_BLO_W         130            /*    "   ,  "   b lo,  "       */
  25. #define CH_BHI_W         131            /*    "   ,  "   b hi,  "       */
  26. #define CH_CLO_W         132            /*    "   ,  "   c lo,  "       */
  27. #define CH_CHI_W         133            /*    "   ,  "   c hi,  "       */
  28. #define CH_ENA_W         135            /* voice channel enable(write)  */
  29. #define CH_AVL_W         136            /* channel a volume, write      */
  30. #define CH_BVL_W         137            /*    "    b   "   ,   "        */
  31. #define CH_CVL_W         138            /*    "    c   "   ,   "        */
  32. #define RG_ENV_W         140            /* envelope register, write     */
  33. #define RG_WAV_W         141            /* waveform register, write     */
  34. #define CONTERM        0X484            /* attributes of console        */
  35.  
  36. #include <stdio.h>                      /* standard io library          */
  37. #include <osbind.h>                     /* GEMDOS,BIOS,XBIOS macros     */
  38. #include <ctype.h>                      /* character type macros        */
  39.  
  40. #define Supexec(a)      xbios(38,a)     /* Exec routine,supervisor mode */
  41.  
  42. /* play contains the numbers to plug into the ST sound chip */
  43. short int play[ MAX_OCT ][ N_P_OCT ] =
  44. {
  45.  { 3906,3689,3472,3189,3048,2906,2728,2551,2411,2272,2160,2049,1923,1817,1712 },
  46.  { 1923,1817,1712,1633,1524,1436,1350,1275,1205,1136,1076,1016,954,902,850 },
  47.  { 954,902,850,807,757,714,675,637,602,568,537,506,477,451,425 },
  48.  { 477,451,425,401,378,358,338,318,301,284,268,253,239,225,212 },
  49.  { 239,225,212,200,189,179,169,159,150,142,134,126,119,112,106 },
  50.  { 119,112,106,100, 94, 89, 84, 79, 75, 71, 67, 63, 59, 54, 51 }
  51. };
  52.  
  53. short int contrl[12],
  54.           intin[256], intout[256],
  55.           ptsin[256], ptsout[256],
  56.           workin[20], workout[100],
  57.           handle, i, j,
  58.           dvar, port_state,
  59.           key_rate,fast_key = 1,
  60.           x_note, y_note, z_note,
  61.           old_note = 0;
  62.  
  63. char c, x_low, x_high, y_low, y_high, z_low, z_high;
  64. char *std_msg = "Press ESC to Exit                   ";
  65.  
  66. short int hold_n = 0,             /* flag-- hold notes            */
  67.           period = 10,            /* period for sound waves       */
  68.           wave = 0,               /* waveform                     */
  69.           vol_a = MAX_VOL,        /* voice a volume               */
  70.           vol_b = MAX_VOL,        /* voice b volume               */
  71.           vol_c = MAX_VOL,        /* voice c volume               */
  72.           oct_a = 2,              /* voice a octave               */
  73.           oct_b = 2,              /* voice b octave               */
  74.           oct_c = 2,              /* voice c octave               */
  75.           tune  = 6,              /* tuning (added to pitch)      */
  76.           tn_a=2,tn_b=2,tn_c=2,   /* tuning for individual voices */
  77.           diss = 3;               /* dissonance level             */
  78.  
  79. int nonoise();                    /* routine to disable noise     */
  80.  
  81. main()
  82. {
  83. /* Check for correct resolution */
  84.      if (Getrez() != 1)
  85.          {
  86.          printf("Sorry, this program must be run in medium resolution.\n");
  87.          exit(0);
  88.          }
  89.  
  90. /* Initialize application */
  91.      appl_init();
  92.      handle=graf_handle( &dvar, &dvar, &dvar, &dvar );
  93.      for ( i = 0;  i < 10;  workin[ i++ ]=1 );  workin[ 10 ] = 2;
  94.      v_opnvwk( workin, &handle, workout );
  95.  
  96. /* Hide mouse, disable keyclick,bell, speed up keyboard repeat rate */
  97.      graf_mouse( MOUS_OFF, 0 );
  98.      Supexec(nonoise);
  99.      key_rate = Kbrate( 0x01,0x01 );
  100.  
  101. /* Initialize sound chip, enable channels a and b */
  102.      sound_off();
  103.      port_state = Giaccess( i, CH_ENA_R  );
  104.      Giaccess( (port_state & 0xc0) | 56, CH_ENA_W );
  105.  
  106. /* Make music */
  107.      play_keys();
  108.  
  109. /* turn off the sound chip, restore initial state */
  110.      Giaccess( port_state, CH_ENA_W );
  111.  
  112. /* restore keyboard repeat rate, close application and exit */
  113.      Kbrate( (key_rate >> 8) & 0x00ff, key_rate & 0x00ff );
  114.      v_clsvwk( handle );
  115.      appl_exit();
  116. }
  117.  
  118. play_keys()
  119. {
  120.      register char c=' ', c2=' ';
  121.  
  122.      keys_screen();
  123.      while (c != ESC_K)
  124.           {
  125.           if (Bconstat(2))
  126.                {
  127.                vol_c = 0;
  128.                c = Bconin(2);    c = tolower(c);
  129.                switch(c)
  130.                     {
  131.                     case '1':
  132.                          locate(22,1);
  133.                          put_msg( "H=>Higher, L=>Lower, RETURN=>Done    ");
  134.                          x_note = play[ oct_a ][ 0 ] + tn_a;
  135.                          y_note = play[ oct_b ][ 0 ] + tn_b;
  136.                          sound_on();
  137.                          do
  138.                               {
  139.                               c2 = Bconin(2);      c2 = tolower(c2);
  140.                               switch(c2)
  141.                                  {
  142.                                  case 'h':
  143.                                    if (tune)
  144.                                        {
  145.                                        tune -= TUNE_INC;
  146.                                        tn_a = tune/(oct_a+1);
  147.                                        tn_b = tune/(oct_b+1);
  148.                                        tn_c = tune/(oct_c+1);
  149.                                        x_note = play[ oct_a ][ 0 ] + tn_a;
  150.                                        y_note = play[ oct_b ][ 0 ] + tn_b;
  151.                                        sound_on();
  152.                                        dsp_vals();
  153.                                        }
  154.                                     break;
  155.                                  case 'l':
  156.                                     if (tune < TUNE_INC*5)
  157.                                         {
  158.                                         tune += TUNE_INC;
  159.                                         tn_a = tune/(oct_a+1);
  160.                                         tn_b = tune/(oct_b+1);
  161.                                         tn_c = tune/(oct_c+1);
  162.                                         y_note = play[ oct_b ][ 0 ] + tn_b;
  163.                                         x_note = play[ oct_a ][ 0 ] + tn_a;
  164.                                         sound_on();
  165.                                         dsp_vals();
  166.                                         }
  167.                                      break;
  168.                                   default:
  169.                                      break;
  170.                                   }
  171.                               } while (c2 != RETURN_K );
  172.                               sound_off();
  173.                               put_msg( std_msg );
  174.                               x_note = old_note;
  175.                               break;
  176.